﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class CustomersEquipmentsCollection : AbstractCollection
    {
        protected static List<CustomersEquipments> _list;

        /// <summary>
        /// Список привязок
        /// </summary>
        public List<CustomersEquipments> CustomersEquipmentsList
        {
            get
            {
                return Entities.CustomersEquipments.ToList();
                if (_list == null)
                    _list = DB.Entities.CustomersEquipments.ToList();
                return _list;
            }
        }

        /// <summary>
        /// Получение списка элементов по идентификатору клиента
        /// </summary>
        /// <param name="customerId">Идентификатор клиента</param>
        /// <returns>Список элементов</returns>
        public List<CustomersEquipments> GetItemsByCustomerId(int customerId)
        {
            return CustomersEquipmentsList.Where(c => c.IdCustomer == customerId).ToList();
        }

        /// <summary>
        /// Список оборудования клиента
        /// </summary>
        /// <param name="idCustomer"></param>
        /// <returns></returns>
        public List<CustomersEquipments> GetCustomersEquipmentsByCustomerId(int idCustomer)
        {
            var result = new List<CustomersEquipments>();
            foreach (CustomersEquipments item in CustomersEquipmentsList)
            {
                if (item.IdCustomer == idCustomer)
                    result.Add(item);
            }
            return result;
        }

        /// <summary>
        /// Поиск оборудования клиента
        /// </summary>
        /// <param name="idCustomer">Идентификатор клиента</param>
        /// <param name="idEquipment">Идентификатор оборудования</param>
        /// <returns>Связка клиент-оборудование</returns>
        public CustomersEquipments GetCustomersEquipmentsbyIdCustomerAndIdEquipment(int idCustomer, int idEquipment)
        {
            if (CustomersEquipmentsList.Count < 1) return null;
            return
                CustomersEquipmentsList.FirstOrDefault(p => p.IdCustomer == idCustomer && p.IdEquipment == idEquipment);
        }

        #region Implementation of IGetItem

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(CustomersEquipmentsList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (CustomersEquipments);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            return _list;
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.CustomersEquipments.Find(itemId);
            return CustomersEquipmentsList.Where(cc => cc.IdCustomersEquipments == itemId);
        }

        #endregion

        #region Implementation of IAddDeleteUpdate

        /// <summary>
        /// Обновление элемента в коллекции
        /// </summary>
        /// <param name="item">Новый элемент</param>
        /// <param name="userLogin">Логин пользователя, который делает изменения</param>
        /// <returns>Результат операции</returns>
        public bool Update(object item, string userLogin)
        {
            var newEquipment = (CustomersEquipments) item;
            if (newEquipment == null)
                return false;


            var Equipment4Update = GetItemByIdFromDatabase(newEquipment.IdCustomersEquipments) as CustomersEquipments;

            if (Equipment4Update == null)
                return true;


            if (newEquipment.IdCustomer != null)
            {
                Equipment4Update.IdCustomer = newEquipment.IdCustomer;
            }

            if (newEquipment.IdEquipment != null)
            {
                Equipment4Update.IdEquipment = newEquipment.IdEquipment;
            }

            if (newEquipment.IssueDate != null)
            {
                Equipment4Update.IssueDate = newEquipment.IssueDate;
            }

            if (newEquipment.EquipmentCount != null)
            {
                Equipment4Update.EquipmentCount = newEquipment.EquipmentCount;
            }

            if (newEquipment.ProductionNumber != null)
            {
                Equipment4Update.ProductionNumber = newEquipment.ProductionNumber;
            }


            return base.Update(Equipment4Update, userLogin);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            _list = null;
        }

        #endregion
    }
}